home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14499 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  91 lines

  1. Path: mujibur.inmind.com!usenet
  2. From: mfinney@inmind.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Doubly Linked List help - please!
  5. Date: 31 Mar 1996 03:54:51 GMT
  6. Organization: In Mind, Inc.
  7. Message-ID: <4jkvmb$dac@mujibur.inmind.com>
  8. References: <internews46B70D427A@argonet.co.uk>
  9. Reply-To: mfinney@inmind.com
  10. NNTP-Posting-Host: finneyman.inmind.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <internews46B70D427A@argonet.co.uk>, Charlotte Tomlinson <eeyore@argonet.co.uk> writes:
  14. >I have implemented a template doubly linked list, the backbones of which
  15. >is as follows:
  16. ..<snip>...
  17. >template <class T> class dllist : public node<T>
  18. >{
  19.  
  20. This is a problem.  A dllist<T> is NOT a node<T>.  A linked list
  21. uses zero or more nodes, but is not itself a node.  It generally is
  22. not meaningful to send an instance of dllist the getNext() or the
  23. getPrev() message.  So dllist should not inherit (pubically or
  24. privately, for that matter) from node<T>.
  25.  
  26. ..<snip>...
  27. >Then I have a class, called fuzzyset, which uses this dll as a container
  28. >for fuzzyel instances:
  29. ..<snip>...
  30. >What I would like to do is, within dllist, to implement a function forEach
  31. >that take a pointer to a function as one argument, and somehow the rest of
  32. >the arguments required by that function, and carries that function out
  33. >over each element of the list. 
  34.  
  35. There are probably two things that you need.  First, you need an
  36. "iterator" class, say dllistIterator, which is a friend of dllist.  This
  37. class should contain a pointer to an instance of dllist and know
  38. enough about the structure of dllist that it can follow the linked
  39. list.  Generally it needs to be able to be reset, to be advanced,
  40. to be retreated, to obtain the element at the cursor and so forth.
  41. The dliterator class should inherit from a general abstract iterator
  42. class.
  43.  
  44. Then, the fuzzyset class needs a function which returns an instance
  45. of an iterator over itself.  This allows the representation of fuzzyset
  46. to be changed later, say to a binary tree.  Then, of course, it would
  47. return an instance of treeIterator instead of dllistIterator.
  48.  
  49. This allows the user of an fuzzyset instance to create any number of
  50. iterators and to "iterate" over its contents.
  51.  
  52. Second, you need a "functor" class which overloads operator() for
  53. objects of type T.  You can provide common functors for the user,
  54. but the user can also create a new subclass of functor to specialized
  55. purposed.  Then you can add a function to fuzzyset which will accept
  56. an instance of a subclass of functor and call it with each element of
  57. fuzzyset.  Since such an instance is an object, it can contain any data
  58. whatsoever -- and therefore it can contain the other parameters to
  59. the function you desire.  And operator() effectively implements that
  60. function.  Thus the fuzzyset class has no knowledge of the function
  61. and so provides a completely generic solution.
  62.  
  63. Example:
  64.  
  65. template <class T> class Functor
  66.    {
  67.    void operator()(T const & aT);
  68.    };
  69.  
  70. template <class T> class ElementCount : public Functor
  71.    {
  72.    private:
  73.       unsigned long count;
  74.    public:
  75.       void operator()(T const & aT) {++count;}
  76.       unsigned long elements() {return count;};
  77.       ElementCount() {count = 0;};
  78.     ~ElementCount();
  79.    };
  80.  
  81. fuzzyset aSet;
  82. ElementCount aCount;
  83.  
  84. aSet.apply(aCount);
  85.  
  86. cout << "fuzzyset contains " << aCount.elements() << " elements" << endl;
  87.  
  88.  
  89. Michael Lee Finney
  90.  
  91.